package com.qfedu.common.core.interceptor;

import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @Classname TimeInterceptor
 * @Description TODO
 * @Date 2025-09-23 15:49
 * @Created by 老任与码
 */
public class TimeInterceptor implements HandlerInterceptor {

    long begin = 0;
    public static ThreadLocal<Long> threadLocal = new ThreadLocal<>();

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        begin = System.currentTimeMillis();
        System.out.println("begin:" + begin);
        // 将数据存入threadlocal
        threadLocal.set(begin);
        return true;
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        long end = System.currentTimeMillis();
        // long v = end - begin;
        // 从threadlocal中取数据
        long v = end - threadLocal.get();
        System.out.println(v);
    }
}
